package com.dalei.vds.settings;

import com.dalei.vds.soap.DebugSoapImpl;
import com.dalei.vds.telephone.net.NetConnection;
import com.dalei.vds.telephone.net.WifiConnection;
import com.dalei.vdserver.shared.soap.DebugSoap;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class ServerSettings {
	
	public static final int SUCCESS = 0x00000001;
	
	public static final int WEB_SERVICE_NOT_AVAILABLE = 0x00000000;
	
	public static final int NETWORK_NOT_AVAILABLE = 0xffffffff;
	
	public static final int INVALID_IP_ADDRESS = 0xfffffffe;
	
	public static final int IP_ADDRESS_NOT_REACHABLE = 0xfffffffd;
	
	public static final int PORT_NOT_OPENED = 0xfffffffc;
	
	public static final int SETTINGS_NOT_FOUND = 0xfffffffb;
	
	public static final int INTERNAL_ERROR = 0xfffffffa;
	
	public static final String HOSTNAME = "hostName";
	
	public static final String IPADDRESS = "ipAddress";
	
	public static final String PORT = "port";
	
	public static final String TIMEOUT = "timeout";
	
	private SharedPreferences sharedPreferences;
	
	private Context context;
	
	public ServerSettings(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
		sharedPreferences = context.getSharedPreferences("ServerSettings", Context.MODE_PRIVATE);
	}
	
	public int debug(String ipAddress, int port, int timeout) {
		WifiConnection wifiConnection = new WifiConnection(context);
		if (wifiConnection.isConnected()) {
			if (NetConnection.isValidIpAddress(ipAddress)) {
				if (NetConnection.isReachable(ipAddress, timeout)) {
					if (NetConnection.isPortOpened(ipAddress, port)) {
						DebugSoap soap = new DebugSoapImpl(ipAddress, port, timeout);
						String msg = soap.debug();
						if (msg != null) {
							if (msg.equals(DebugSoap.SUCCESS)) {
								return SUCCESS;
							} else {
								return WEB_SERVICE_NOT_AVAILABLE;
							}
						} else {
							return INTERNAL_ERROR;
						}
					} else {
						return PORT_NOT_OPENED;
					}
				} else {
					return IP_ADDRESS_NOT_REACHABLE;
				}
			} else {
				return INVALID_IP_ADDRESS;
			}
		} else {
			return NETWORK_NOT_AVAILABLE;
		}
	}
	
	public int debug() {
		WifiConnection wifiConnection = new WifiConnection(context);
		if (wifiConnection.isConnected()) {
			if (isSet()) {
				String ipAddress = getIpAddress();
				int port = getPort();
				int timeout = getTimeout();
				if (NetConnection.isValidIpAddress(ipAddress)) {
					if (NetConnection.isReachable(ipAddress, timeout)) {
						if (NetConnection.isPortOpened(ipAddress, port)) {
							DebugSoap soap = new DebugSoapImpl(ipAddress, port, timeout);
							String msg = soap.debug();
							if (msg != null) {
								if (msg.equals(DebugSoap.SUCCESS)) {
									return SUCCESS;
								} else {
									return WEB_SERVICE_NOT_AVAILABLE;
								}
							} else {
								return INTERNAL_ERROR;
							}
						} else {
							return PORT_NOT_OPENED;
						}
					} else {
						return IP_ADDRESS_NOT_REACHABLE;
					}
				} else {
					return INVALID_IP_ADDRESS;
				}
			} else {
				return SETTINGS_NOT_FOUND;
			}
		} else {
			return NETWORK_NOT_AVAILABLE;
		}
	}
	
	public boolean saveSettings(String ipAddress, int port, int timeout) {
		Editor editor = sharedPreferences.edit();
		editor.putString(IPADDRESS, ipAddress);
		editor.putInt(PORT, port);
		editor.putInt(TIMEOUT, timeout);
		return editor.commit();
	}
	
	public boolean isSet() {
		return sharedPreferences.contains(IPADDRESS)
				&& sharedPreferences.contains(PORT)
				&& sharedPreferences.contains(TIMEOUT);
	}
	
	public String getHostName() {
		if (isSet()) {
			DebugSoap debugSoap = new DebugSoapImpl(context);
			return debugSoap.getHostName();
		} else {
			return null;
		}
	}
	
	public String getIpAddress() {
		return sharedPreferences.getString(IPADDRESS, null);
	}
	
	public int getPort() {
		return sharedPreferences.getInt(PORT, -1);
	}
	
	public int getTimeout() {
		return sharedPreferences.getInt(TIMEOUT, -1);
	}
	
	public boolean clearSettings() {
		Editor editor = sharedPreferences.edit();
		editor.clear();
		return editor.commit();
	}
}
